package cn.edu.leetcode.查找;

public class 二分查找 {

    public static void main(String[] args) {
        int[] srcArray = {1, 2, 3, 4, 5, 6};
        int des = 6;

        System.out.println(binarySearch(srcArray, des));
    }

    public static int binarySearch(int[] srcArray, int des) {
        int low = 0;
        int height = srcArray.length - 1;
        while (low <= height) {
            int middle = (low + height) / 2;
            if (des == srcArray[middle]) {
                return middle;
            } else if (des < srcArray[middle]) {
                height = middle - 1;
            } else {
                low = middle + 1;
            }
        }
        return -1;
    }
}
